package com.lc.redis.redisTemplate;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.Set;

@SpringBootTest
public class SetType {

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 关注
     */
    @Test
    public void like(){

        //可以根据如下api查询当前用户是否点赞，如果点赞了就可以取消点赞，如果没有就添加点赞
        Boolean member = redisTemplate.opsForSet().isMember("article:1", 1);
        if(member){
            redisTemplate.opsForSet().remove("article:1",1);
        }else {
            redisTemplate.opsForSet().add("article:1", 1);
        }
        System.out.println(member);
//        Boolean member2 = redisTemplate.opsForSet().isMember("article:1", 1);
//        System.out.println(member2);
    }

    /**
     * 查看被那些人关注
     */
    @Test
    public void MutualFriends(){
        //假设value的用户id
        redisTemplate.opsForSet().add("article:1", 1);
        redisTemplate.opsForSet().add("article:1",2);
        Set members = redisTemplate.opsForSet().members("article:1");
        System.out.println(members);
    }

    /**
     * 共同关注
     */
    @Test
    public void look(){
        redisTemplate.opsForSet().add("article:2", 1);
        redisTemplate.opsForSet().add("article:2",3);
        Set intersect = redisTemplate.opsForSet().intersect("article:2", "article:1");
        System.out.println(intersect);
    }
}
